<template>
  <div class="home">

  </div>
</template>

<script>
// @ is an alias to /src

import axios from 'axios'
import { Promise } from 'q';
import { setTimeout } from 'timers';

export default {
  name: 'axios3-4',
  components: {

  },
  created () {
    axios.interceptors.request.use(config => {
      return config
    }, err => {
      return Promise.reject(err)
    })
    axios.interceptors.response.ues(res => {
      return res
    }, err => {
      return Promise.reject(err)
    })
    axios.get('/data.json'), then((res) => {
      console.log(res)
    }).catch(err => {
      cnosole.log(err)
    })
    //例子：在实际的开发过程中，添加同一的错误处理
    let instance = axios.create({})
    instance.interceptors.request(config => {
      return config
    }, err => {
      // 请求错误返回的状态码，401超时，404 not found
      $('#model').show()
      setTimeout(() => {
        $('#model').hide()
      }, 2000)
    })
    instance.interceptors.response.ues(res => {
      return res
    }, err => {
      $('#model').show()
      setTimeout(() => {
        $('#model').hide()
      }, 2000)
      //响应错误处理 一般http状态码5开头，500系统错误，502系统重启
      return Promise.reject(err)
    })
    instance.get('/data.json').then(res=>{
      console.log(res)
    }).catch(err=>{
      console.log(err)
    })
  },
}
</script>
